package de.shortys.myhome.api.persistence;

import org.hibernate.MappingException;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class HibernateConfiguration {

	private static final String HIBERNATE_CONFIG_FILEPATH = "/de/shortys/myhome/api/persistence/Hibernate.cfg.xml";
	private Configuration cfg;

	public PersistenceTemplate getPersistenceTemplate() {
		System.out.println("template erstellt");
		return new PersistenceTemplate(new HibernateTemplate(cfg.buildSessionFactory(), true));
	}

	public HibernateConfiguration() {
		cfg = new Configuration().configure(HIBERNATE_CONFIG_FILEPATH);
	}

	public void addResource(String resource) throws MappingException {
		cfg = cfg.addResource(resource);
		final SchemaExport schemaExport = new SchemaExport(cfg);
		schemaExport.execute(false, true, false, true);
	}

}
